package com.ruiysoft.controller.sys.api.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruiysoft.controller.sys.res.UserShopResponse;
import com.ruiysoft.entity.sys.SysMicroShop;
import com.ruiysoft.entity.sys.SysShopPersonnel;
import com.ruiysoft.entity.sys.SysShopPersonnelRelation;
import com.ruiysoft.entity.sys.SysUser;
import com.ruiysoft.entity.sys.SysUserShopRelation;
import com.ruiysoft.service.sys.ISysMicroShopService;
import com.ruiysoft.service.sys.ISysShopPersonnelRelationService;
import com.ruiysoft.service.sys.ISysUserService;
import com.ruiysoft.service.sys.ISysUserShopRelationService;
import com.ruiysoft.service.sys.redis.IRedisSysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 化增光 on 2019/1/4.
 */
@Service
public class SysUserApiService {

    private static final Logger log = LoggerFactory.getLogger(SysUserApiService.class);

    @Resource
    ISysUserShopRelationService iSysUserShopRelationService;
    @Resource
    ISysMicroShopService iSysMicroShopService;

    @Resource
    IRedisSysUserService iRedisSysUserService;

    @Resource
    ISysUserService iSysUserService;

    @Resource
    ISysShopPersonnelRelationService iSysShopPersonnelRelationService;

//    @Resource
//    IRedisSysMicroShopService iRedisSysMicroShopService;


    public List<UserShopResponse> getUserShops(Long id) {
        List<UserShopResponse> responses = new ArrayList<>();
        List<SysUserShopRelation> relations = iSysUserShopRelationService.list(new QueryWrapper<SysUserShopRelation>().eq("user_id", id));
        relations.stream().forEach(e -> {
            SysMicroShop shop = iSysMicroShopService.selectSysMicroShopById(e.getShopId());
            if (null != shop) {
                responses.add(new UserShopResponse().setId(shop.getId()).setShopImg(shop.getShopImg()).setShopName(shop.getShopName()));
            }
        });
        return responses;
    }

    public SysUser getShopkeeper(Long shopId) {
        SysUserShopRelation relation = iSysUserShopRelationService.getOne(new QueryWrapper<SysUserShopRelation>().eq("shop_id", shopId));
        Assert.notNull(relation, "未获取到店主信息");
        return iRedisSysUserService.selectSysUserById(relation.getUserId());
    }

    public IPage<SysMicroShop> getAllShops(Integer pageStart, Integer pageSize) {
        return iSysMicroShopService.page(new Page<>(pageStart, pageSize), new QueryWrapper<SysMicroShop>().orderBy(false, false, "create_time"));
    }

    public SysShopPersonnel getSysShopPersonnel(Long personnelId) {
        return iRedisSysUserService.selectSysShopPersonnelById(personnelId);
    }

    public SysShopPersonnel getSysShopPersonnelByShopId(Long shopId) {
        return getSysShopPersonnel(getShopkeeper(shopId).getPersonnelId());
    }

    public void clearCacheByUserName(String userName) {
        iRedisSysUserService.clearCacheByUserName(userName);
    }

    public void clearCacheById(Long id) {
//        iSysUserService.deleteById(id);
//        List<SysUserShopRelation> rList = iSysUserShopRelationService.selectList(new EntityWrapper<SysUserShopRelation>().eq("user_id",id));
//        if (!rList.isEmpty()){
//            rList.stream().forEach(e ->{
//                iSysMicroShopService.deleteById(e.getShopId());
//            });
//        }
//        iSysUserShopRelationService.delete(new EntityWrapper<SysUserShopRelation>().eq("user_id",id));
    }

    @Transactional
    public void deleteSysUserById(Long id) {
        log.error("开始清理用户数据");
        SysUser sysUser = iSysUserService.getById(id);
        if (null == sysUser) {
            return;
        }
        log.error("删除用户：---》" + JSON.toJSONString(sysUser));
        iSysUserService.removeById(id);
        iRedisSysUserService.clearCacheById(id, sysUser.getPersonnelId());
        iRedisSysUserService.clearCacheByUserName(sysUser.getUserName());
//        iRedisSysUserService.clearSysShopPersonnelById(sysUser.getPersonnelId());
        List<SysUserShopRelation> rList = iSysUserShopRelationService.list(new QueryWrapper<SysUserShopRelation>().eq("user_id", id));
        log.error("删除用户店铺关系：---》" + JSON.toJSONString(rList));
        iSysUserShopRelationService.remove(new QueryWrapper<SysUserShopRelation>().eq("user_id", id));
        if (!rList.isEmpty()) {
            rList.stream().forEach(e -> {
                SysMicroShop microShop = iSysMicroShopService.getById(e.getShopId());
                if (null != microShop) {
                    log.error("删除店铺信息：---》" + JSON.toJSONString(microShop));
                }
                iSysMicroShopService.removeById(e.getShopId());
                iSysMicroShopService.clearSysMicroShopById(e.getId());
                List<SysShopPersonnelRelation> prList = iSysShopPersonnelRelationService.list(new QueryWrapper<SysShopPersonnelRelation>().eq("shop_id", e.getShopId()));
                log.error("删除店铺人员关系：---》" + JSON.toJSONString(prList));
                iSysShopPersonnelRelationService.remove(new QueryWrapper<SysShopPersonnelRelation>().eq("shop_id", e.getShopId()));
            });
        }
    }

    public UserShopResponse selectUserShopInfoByShopId(Long shopId) {
        UserShopResponse response = new UserShopResponse();
        SysUserShopRelation relation = iSysUserShopRelationService.getOne(new QueryWrapper<SysUserShopRelation>().eq("shop_id", shopId));
        if (null == relation) {
            return response;
        }
        SysUser user = iSysUserService.getById(relation.getUserId());
        if (null != user) {
            response.setRealName(user.getRealName())
                    .setUserName(user.getUserName())
                    .setMobile(user.getMobile());
        }
        return response;
    }
}
